﻿@using CommandLib
@model BackEnd.Models.ReportLoginLogModel
@{
    ViewBag.Title = "LoginLog";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Login log</h2>    
<table width="50%">    
    <tr>
        <td class="editor-label">@Html.LabelFor(m=>m.Email)</td>
        <td>@Html.TextBoxFor(m=>m.Email)</td>
        <td></td>
        <td></td>
    </tr>  
    <tr>
        <td class="editor-label">
            @Html.LabelFor(m => m.FromDate) 
        </td>
        <td class="editor-field">
            @Html.TextBoxFor(m => m.FromDate)
        </td>
        <td class="editor-label">
            @Html.LabelFor(m => m.ToDate) 
        </td>
        <td class="editor-field">
            @Html.TextBoxFor(m => m.ToDate)
        </td>
    </tr>
    <tr>
        <td class="editor-label">@Html.LabelFor(m=>m.Status)</td>
        <td>
            <select id="Status" name="Status">
                <option>----All----</option>
                <option value="@Constants.LoginStatus.SUCCESS">Success</option>
                <option value="@Constants.LoginStatus.ERROR">Error</option>
            </select>
        </td>
        <td></td>
        <td></td>
    </tr>  
    <tr>
        <td></td>
        <td align="left"  colspan="3">            
            <a class="bt_red" href="#" onclick="Search(1);" style="float:left"><span class="bt_red_lft"></span><strong>Search</strong><span class="bt_red_r"></span></a>  
        </td>
    </tr>
    <tr>
        <td colspan="2"></td>
    </tr>
    <tr>
        <td colspan="2">
            <div id="divToTalRecord">Total record :</div>
        </td>
    </tr>
</table>
<table id="rounded-corner">
    <thead>
        <tr>        
            <th class="rounded-company" scope="col" style="width: 30px"></th>
            <th class="rounded" scope="col" style="width: 100px">Account Id</th>
            <th class="rounded" scope="col" style="width: 400px">Email</th>
            <th class="rounded" scope="col" style="width: 100px">Password</th>
            <th class="rounded" scope="col" style="width: 200px">Create Date</th>
            <th class="rounded" scope="col" style="width: 100px">Ip</th>
            <th class="rounded-q4" scope="col">Status</th>
        </tr>
    </thead>  
</table>
<div id="divPager"></div>
<script src="~/Scripts/ckeditor/ckeditor.js"></script>
<script src="~/Scripts/ckeditor/adapters/jquery.js"></script> 
<script>
    $(function () {
        $('#FromDate').datepicker();
        $('#ToDate').datepicker();
    });
    
    function Search(PageIndex) {
        var URL = '/LoginLog/Search';
        $.ajax({
            url: URL,
            type: "POST",
            data: {                
                email: $('#Email').val(),
                fromDate: $('#FromDate').val(),
                toDate: $('#ToDate').val(),
                status: $('#Status').val(),
                pageIndex: PageIndex,
                pageSize: 50
            },
            success: function (data) {
                $('#divToTalRecord').html('Total record :'+data.TotalRecord);
                TotalPage = Math.ceil(data.TotalRecord / 50);
                if (TotalPage > 0) {
                    $("#divPager").show();
                } else {
                    $("#divPager").hide();
                }
                $("tr[refer='trContent']").remove();
                var $table = $('#rounded-corner');
                $.each(data.ListItem, function (i, item) {
                    var strHtml = "";
                    var createDate = item.CreateDate == null ? '' : myDateFormatter(parseInt(item.CreateDate.substr(6)));
                    strHtml += "<tr refer='trContent'>";
                    strHtml += "<td> " + ((PageIndex - 1) * 50 + i + 1) + " </td>";
                    strHtml += "<td> " + item.AccountId + " </td>";
                    strHtml += "<td> " + item.Email + " </td>";
                    strHtml += "<td> " + item.PassWord + " </td>";
                    strHtml += "<td> " + createDate + " </td>";
                    strHtml += "<td> " + item.Ip + " </td>";
                    if (item.Status == @Constants.LoginStatus.SUCCESS) {
                        strHtml += "<td>Success</td>";
                    } else {
                        strHtml += "<td>Error</td>";
                    }
                    strHtml += "</tr>";
                    $table.append(strHtml);
                });
                $("#divPager").pager({ pagenumber: PageIndex, pagecount: TotalPage, buttonClickCallback: Search });
            },
            complete: function (o1, o2) {
            }
        });
    }
</script>
